<template>
  <view>
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="true">
      <swiper-item v-for="(item,i) in swiperList" :key="i">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 导航栏 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item,i) in navList" :key="i" @click="navClickHandler(item)">
        <image class="nav-img" :src="item.image_src"></image>
      </view>
    </view>
    <!-- 楼层 -->
    <view class="floor-list">
      <!-- 每个楼层的item项 -->
      <view class="floor-item" v-for="(item,i) in floorList" :key="i">
        <!-- 楼层的标题图片 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <!-- 楼层内容图片盒子 -->
        <view class="floor-image-box">
          <!-- 楼层左侧大图盒子 -->
          <navigator class="left-image-box" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{ width:item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
          </navigator>
          <!-- 楼层右侧四张小图的盒子 -->
          <view class="right-image-box">
            <navigator class="right-image-item" v-for="(item2,i2) in item.product_list.slice(1)" :key="i2" :url="item2.url">
            <image :src="item2.image_src" :style="{width:item2.image_width + 'rpx'}" mode="widthFix"></image>
          </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: []
      }
    },

    onLoad() {
      this.getSwiperList();
      this.getNavList();
      this.getFloorList()
    },
    methods: {
      async getSwiperList() {
        const { data:res } = await uni.$http.get(`https://api-ugo-web.itheima.net/api/public/v1/home/swiperdata`);
        //网络请求失败了
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        //请求成功，为data中的轮播图赋值
        this.swiperList = res.message;
      },
      async getNavList() {
        const { data:res } = await uni.$http.get(`https://api-ugo-web.itheima.net/api/public/v1/home/catitems`);
        //网络请求失败了
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        //请求成功，为data中的轮播图赋值
        this.navList = res.message;
      },
      async getFloorList() {
        const { data:res } = await uni.$http.get(`https://api-ugo-web.itheima.net/api/public/v1/home/floordata`);
        //网络请求失败了
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        //修改url
        res.message.forEach(floor => {
          floor.product_list.forEach(prod=>{
            prod.url='/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
          })
        })
        //请求成功，为data中的轮播图赋值
        this.floorList = res.message;
      },
      navClickHandler(item) {
        if (item.name == '分类') {
          uni.switchTab({
            url: "/pages/cate/cate"
          })
        }
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;
    .swiper-item,
    image {
      height: 100%;
      width: 100%;
    }
  }

  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 15px 0
  }

  .nav-img {
    height: 140rpx;
    width: 128rpx;
  }
  
  .floor-title {
    height:60rpx;
    width: 100%;
  }
  .right-image-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-evenly;
  }
  .floor-image-box {
    display: flex;
    padding-left: 14rpx;
  }
</style>
